<?php

namespace app\api\model;

use think\Model;

/**
 * Beginner
 */
class Beginner extends Model
{
    // 表名
    protected $name = 'guide_beginner';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    protected static function onAfterInsert($model): void
    {
        if ($model->weigh == 0) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }

    public function getContentAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }

    public function getImageAttr($value){
        return full_url($value);
    }


    public function getGuideBeginner(){
        $list = self::where(['status' => 1])->order('weigh desc')
            ->field('id,name,look_nums,content,create_time')
            ->paginate(10);

        foreach ($list as &$value){
            $value['create_time'] = date("Y.m.d H:i",$value['create_time']);
        }

        return api_message($list);
    }
}